package com.zh.rent.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.rent.dao.InvitationDao;
import com.zh.rent.pojo.Account;
import com.zh.rent.pojo.BuyAccount;
import com.zh.rent.pojo.Invitation;
import com.zh.rent.pojo.StandbyAccount;
import com.zh.rent.service.InvitationService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(isolation = Isolation.READ_COMMITTED)
public class InvitationServiceImpl implements InvitationService {
    @Autowired
    InvitationDao invitationDao;


    @Override
    public int insertInvAll(List<Invitation> list) {
        return invitationDao.insertinvAll(list);
    }

    @Override
    public PageInfo<Map<String, Object>> selInvAll(Integer pageNum,Integer pageSeiz, Map<String,Object> map) {
        PageHelper.startPage(pageNum,pageSeiz);
        List<Map<String, Object>> list = invitationDao.selInvAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public int updateInv(Invitation invitation) {
        return invitationDao.updateInv(invitation);
    }

    @Override
    public int delInvBccAll(List<Integer> list) {
        return invitationDao.delInvBccAll(list);
    }

    @Override
    public int delInvBccid(Integer Invitationid) {
        return invitationDao.delInvBccid(Invitationid);
    }


    @Override
    public Map<String,List<Invitation>> selAll() {
        List<Invitation> list1 = invitationDao.buyAll();
        Map<String,List<Invitation>> map = new HashMap<>();
        for (Invitation buyAccount : list1) {
            if(map==null) {
                List<Invitation> list = new ArrayList<>();
                list.add(buyAccount);
                map.put(buyAccount.getAccountType(),list);
            }
            boolean bo = true;
            Iterator<Map.Entry<String, List<Invitation>>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, List<Invitation>> next = iterator.next();
                if(next.getKey().equalsIgnoreCase(buyAccount.getAccountType())){
                    bo=false;
                    next.getValue().add(buyAccount);
                }
            }
            if(bo){
                List<Invitation> list = new ArrayList<>();
                list.add(buyAccount);
                map.put(buyAccount.getAccountType(),list);
            }
        }
        return map;
    }

    @Override
    public List<Invitation> invDissel(Map<String, Object> map) {
        return invitationDao.invDissel(map);
    }

    @Override
    public Invitation invDis(Map<String, Object> map) {
        return invitationDao.invDis(map);
    }

    @Override
    public int updateInvDis(Map<String,Object> map) {
        return invitationDao.updateInvDis(map);
    }

    @Override
    public List<Map<String,Object>> selVoucherInvAll(String Voucher) {
        return invitationDao.selVoucherInvAll(Voucher);
    }

    @Override
    public Invitation selCodeInv(String code) {
        return invitationDao.selCodeInv(code);
    }

    @Override
    public int updateType(Integer Invitationid,Timestamp timestamp) {
        return invitationDao.updateType(Invitationid,timestamp);
    }

    @Override
    public int updatelogNum(int Invitationid) {
        return invitationDao.updatelogNum( Invitationid);
    }

    @Override
    public List<Map<String, Object>> invselType(String invselType) {
        return invitationDao.invselType(invselType);
    }

    @Override
    public int upvtRenew(Integer residue, String InvitationCode) {
        return invitationDao.upvtRenew(residue,InvitationCode);
    }

    @Override
    public int uplogres(Integer logNum) {
        return invitationDao.uplogres(logNum);
    }

    @Override
    public List<Invitation> selInvall() {
        return invitationDao.selInvall();
    }

    @Override
    public Invitation upInvgive(Integer Invitationid) {
        return invitationDao.upInvgive(Invitationid);
    }

    @Override
    public int uplog(Integer logNum) {
        return invitationDao.uplog(logNum);
    }
}
